/**
 * @param 	x 字符串
 * @return 	两位小数的数字
 */
function ToDecimal2(x) {  
    var f = parseFloat(x);  
    if (isNaN(f)) {  
        return false;  
    }  
    var f = Math.round(x*100)/100;  
    var s = f.toString();  
    var rs = s.indexOf('.');  
    if (rs < 0) {
        rs = s.length;  
        s += '.';  
    }  
    while (s.length <= rs + 2) {  
        s += '0';  
    }  
    return s;  
}

/**
 * 
 * @return 	返回一个包含当前页面url中所有参数的的对象
 */
function GetCFPUrlParms() {
    var args=new Object();
    var query=location.search.substring(1); 
    var pairs=query.split("&");
    for(var i=0;i<pairs.length;i++) {   
        var pos=pairs[i].indexOf('=');
        if(pos==-1)   continue;
        var argname=pairs[i].substring(0,pos);
        var value=pairs[i].substring(pos+1);
        args[argname]=unescape(value);
    }
    return args;
}

/**
 * 在文档中寻找分页数据，并返回当前页面在分页中的位置、最大页码、分页查询参数以及是否存在分页
 
 * @param		doc 			html文档根对象或者完整的html页面代码
 * @param		pagePath	分页链接的前缀，用于识别分页链接
 * @return 	返回一个包含当前页面url中所有参数的的对象
 */
function GetPagingInfo(doc, pagePath) {
  var pagingBar = $(doc).find('body > div.content.clearfix > div.product_warp > div.product_mod > div.pagination_mod.tr > ul');
  var currentPageLink = pagingBar.find('li.current > a');
  var currentPage = parseInt(currentPageLink.text());
  var maxPage = 1;
  var pageUrl;
  pagingBar.find("li > a[href^='" + pagePath + "']")
    .each(function(index){
			var pageNum = parseInt($(this).text());
			if ( pageNum >= maxPage ) {
				maxPage = pageNum;
				pageUrl = $(this).attr('href');
			}
	});
	var params = GetCFPUrlParms(pageUrl);
	return  {
	  currentPage: currentPage,
	  maxPage: maxPage,
	  params: params,
	  pagingFlag: currentPageLink.size() > 0
	};
}

/**
 * 根据平台基准利率表计算计划持有期到期后的利率，通过这两个利差计算年化收益率
 *
 * 按计划执行的年化收益率计算方法：
 * 1. 计算套利的年化收益：x天利差 * ( 12 / (计划持有天数 / 30 ))
 * 2. 按计划执行的年化收益率 = 产品本身的年化收益率 + 套利的年化收益
 *
 * @currentRate 当前转让利率
 * @remainingMaturity 产品剩余期限
 * @benchmarkInterestRates 平台基准利率表
 * @planHoldPeriod 计划持有期限
 * @return 返回扣除平台费后计划持有期限到期时的利差、出售后的总利润等
 */
function CalcBenchmarkInterestRate(currentRate, remainingMaturity, benchmarkInterestRates, planHoldPeriod) {
	for ( var i = 0; i < benchmarkInterestRates.length; i++ ) {
		var benchmarkInterestRate = benchmarkInterestRates[i];
		if ( (remainingMaturity - planHoldPeriod) <= benchmarkInterestRate.days ) {
			var sellDay = remainingMaturity - planHoldPeriod;
			var sellRate = benchmarkInterestRate.annualizedRate - benchmarkInterestRate.perday * (benchmarkInterestRate.days - sellDay);
			var margin = (currentRate - sellRate);
			var totalInterestMargin = margin*(sellDay/360) - 0.002*(sellDay/360);
			return {
					sellDay: sellDay, 
					sellRate: sellRate, 
					margin: margin,
					currentRate:currentRate,
					remainingMaturity:remainingMaturity,
					totalInterestMargin: totalInterestMargin,
					totalAnnualizedRateofReturn: (totalInterestMargin * (12 / ( planHoldPeriod / 30)) + currentRate)
				};
		}
	}
	return {};
}

function GetBenchmarkInterestRate(remainingMaturity, benchmarkInterestRates) {
	for ( var i = 0; i < benchmarkInterestRates.length; i++ ) {
		var benchmarkInterestRate = benchmarkInterestRates[i];
		if ( remainingMaturity <= benchmarkInterestRate.days ) {
			var rate = benchmarkInterestRate.annualizedRate - benchmarkInterestRate.perday * (benchmarkInterestRate.days - remainingMaturity);
			return rate;
		}
	}
	return {};
}